<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Cookbook &raquo; Composable Tasking | Taskflow QuickStart</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,400i,600,600i%7CSource+Code+Pro:400,400i,600" />
  <link rel="stylesheet" href="m-dark+documentation.compiled.css" />
  <link rel="icon" href="favicon.ico" type="image/vnd.microsoft.icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta name="theme-color" content="#22272e" />
</head>
<body>
<header><nav id="navigation">
  <div class="m-container">
    <div class="m-row">
      <span id="m-navbar-brand" class="m-col-t-8 m-col-m-none m-left-m">
        <a href="https://taskflow.github.io"><img src="taskflow_logo.png" alt="" />Taskflow</a> <span class="m-breadcrumb">|</span> <a href="index.html" class="m-thin">QuickStart</a>
      </span>
      <div class="m-col-t-4 m-hide-m m-text-right m-nopadr">
        <a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
          <path id="m-doc-search-icon-path" d="m6 0c-3.31 0-6 2.69-6 6 0 3.31 2.69 6 6 6 1.49 0 2.85-0.541 3.89-1.44-0.0164 0.338 0.147 0.759 0.5 1.15l3.22 3.79c0.552 0.614 1.45 0.665 2 0.115 0.55-0.55 0.499-1.45-0.115-2l-3.79-3.22c-0.392-0.353-0.812-0.515-1.15-0.5 0.895-1.05 1.44-2.41 1.44-3.89 0-3.31-2.69-6-6-6zm0 1.56a4.44 4.44 0 0 1 4.44 4.44 4.44 4.44 0 0 1-4.44 4.44 4.44 4.44 0 0 1-4.44-4.44 4.44 4.44 0 0 1 4.44-4.44z"/>
        </svg></a>
        <a id="m-navbar-show" href="#navigation" title="Show navigation"></a>
        <a id="m-navbar-hide" href="#" title="Hide navigation"></a>
      </div>
      <div id="m-navbar-collapse" class="m-col-t-12 m-show-m m-col-m-none m-right-m">
        <div class="m-row">
          <ol class="m-col-t-6 m-col-m-none">
            <li><a href="pages.html">Handbook</a></li>
            <li><a href="namespaces.html">Namespaces</a></li>
          </ol>
          <ol class="m-col-t-6 m-col-m-none" start="3">
            <li><a href="annotated.html">Classes</a></li>
            <li><a href="files.html">Files</a></li>
            <li class="m-show-m"><a href="#search" class="m-doc-search-icon" title="Search" onclick="return showSearch()"><svg style="height: 0.9rem;" viewBox="0 0 16 16">
              <use href="#m-doc-search-icon-path" />
            </svg></a></li>
          </ol>
        </div>
      </div>
    </div>
  </div>
</nav></header>
<main><article>
  <div class="m-container m-container-inflatable">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <h1>
          <span class="m-breadcrumb"><a href="Cookbook.html">Cookbook</a> &raquo;</span>
          Composable Tasking
        </h1>
        <nav class="m-block m-default">
          <h3>Contents</h3>
          <ul>
            <li><a href="#ComposeATaskflow">Compose a Taskflow</a></li>
            <li><a href="#CreateAModuleTask">Create a Module Task</a></li>
            <li><a href="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></li>
          </ul>
        </nav>
<p>Composition is a key to improve the programmability of a complex workflow. This chapter describes how to create a large parallel graph through composition of modular and reusable blocks that are easier to optimize.</p><section id="ComposeATaskflow"><h2><a href="#ComposeATaskflow">Compose a Taskflow</a></h2><p>A powerful feature of <a href="classtf_1_1Taskflow.html" class="m-doc">tf::<wbr />Taskflow</a> is its <em>composable</em> interface. You can break down a large parallel workload into smaller pieces each designed to run a specific task dependency graph. This largely facilitates the <em>modularity</em> of writing a parallel task program.</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="c1">// f1 has three independent tasks</span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">f1</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;F1&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;F1 TaskA</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;F1 TaskB</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f1</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;F1 TaskC</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w"> </span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w"> </span><span class="n">f1A</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f1A&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w"> </span><span class="n">f1B</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f1B&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">10</span><span class="o">:</span><span class="w"> </span><span class="n">f1C</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f1C&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="n">f1A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1C</span><span class="p">);</span><span class="w"></span>
<span class="mi">12</span><span class="o">:</span><span class="w"> </span><span class="n">f1B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1C</span><span class="p">);</span><span class="w"></span>
<span class="mi">13</span><span class="o">:</span><span class="w"></span>
<span class="mi">14</span><span class="o">:</span><span class="w"> </span><span class="c1">// f2A ---</span>
<span class="mi">15</span><span class="o">:</span><span class="w"> </span><span class="c1">//        |----&gt; f2C ----&gt; f1_module_task ----&gt; f2D</span>
<span class="mi">16</span><span class="o">:</span><span class="w"> </span><span class="c1">// f2B --- </span>
<span class="mi">17</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Taskflow</span><span class="w"> </span><span class="n">f2</span><span class="p">;</span><span class="w"></span>
<span class="mi">18</span><span class="o">:</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;F2&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">19</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2A</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;  F2 TaskA</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">20</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2B</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;  F2 TaskB</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">21</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2C</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;  F2 TaskC</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">22</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f2D</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">emplace</span><span class="p">([</span><span class="o">&amp;</span><span class="p">](){</span><span class="w"> </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;  F2 TaskD</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="mi">23</span><span class="o">:</span><span class="w"> </span>
<span class="mi">24</span><span class="o">:</span><span class="w"> </span><span class="n">f2A</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f2A&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">25</span><span class="o">:</span><span class="w"> </span><span class="n">f2B</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f2B&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">26</span><span class="o">:</span><span class="w"> </span><span class="n">f2C</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f2C&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">27</span><span class="o">:</span><span class="w"> </span><span class="n">f2D</span><span class="p">.</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;f2D&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">28</span><span class="o">:</span><span class="w"></span>
<span class="mi">29</span><span class="o">:</span><span class="w"> </span><span class="n">f2A</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2C</span><span class="p">);</span><span class="w"></span>
<span class="mi">30</span><span class="o">:</span><span class="w"> </span><span class="n">f2B</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2C</span><span class="p">);</span><span class="w"></span>
<span class="mi">31</span><span class="o">:</span><span class="w"></span>
<span class="mi">32</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">f1_module_task</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">f1</span><span class="p">).</span><span class="n">name</span><span class="p">(</span><span class="s">&quot;module&quot;</span><span class="p">);</span><span class="w"></span>
<span class="mi">33</span><span class="o">:</span><span class="w"> </span><span class="n">f2C</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f1_module_task</span><span class="p">);</span><span class="w"></span>
<span class="mi">34</span><span class="o">:</span><span class="w"> </span><span class="n">f1_module_task</span><span class="p">.</span><span class="n">precede</span><span class="p">(</span><span class="n">f2D</span><span class="p">);</span><span class="w"></span>
<span class="mi">35</span><span class="o">:</span><span class="w"></span>
<span class="mi">36</span><span class="o">:</span><span class="w"> </span><span class="n">f2</span><span class="p">.</span><span class="n">dump</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="p">);</span><span class="w"></span></pre><div class="m-graph"><svg style="width: 31.600rem; height: 31.100rem;" viewBox="0.00 0.00 316.00 311.00">
<g transform="scale(1 1) rotate(0) translate(4 307)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-295 150,-295 150,-8 8,-8"/>
<text text-anchor="middle" x="79" y="-283" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="158,-152 158,-295 300,-295 300,-152 158,-152"/>
<text text-anchor="middle" x="229" y="-283" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="115" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="79" cy="-178" rx="27" ry="18"/>
<text text-anchor="middle" x="79" y="-175.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60&#45;&gt;p0x7f816f402d80</title>
<path d="M106.65,-232.76C102.29,-224.28 96.85,-213.71 91.96,-204.2"/>
<polygon points="94.99,-202.44 87.3,-195.15 88.77,-205.64 94.99,-202.44"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="140.5,-124 21.5,-124 17.5,-120 17.5,-88 136.5,-88 140.5,-92 140.5,-124"/>
<polyline points="136.5,-120 17.5,-120 "/>
<polyline points="136.5,-120 136.5,-88 "/>
<polyline points="136.5,-120 140.5,-124 "/>
<text text-anchor="middle" x="79" y="-103.5" font-family="Helvetica,sans-Serif" font-size="10.00">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80&#45;&gt;p0x7f816f402fa0</title>
<path d="M79,-159.7C79,-151.98 79,-142.71 79,-134.11"/>
<polygon points="82.5,-134.1 79,-124.1 75.5,-134.1 82.5,-134.1"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="43" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70&#45;&gt;p0x7f816f402d80</title>
<path d="M51.35,-232.76C55.71,-224.28 61.15,-213.71 66.04,-204.2"/>
<polygon points="69.23,-205.64 70.7,-195.15 63.01,-202.44 69.23,-205.64"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="79" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="79" y="-31.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0&#45;&gt;p0x7f816f402e90</title>
<path d="M79,-87.7C79,-79.98 79,-70.71 79,-62.11"/>
<polygon points="82.5,-62.1 79,-52.1 75.5,-62.1 82.5,-62.1"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="265" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="265" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="229" cy="-178" rx="27" ry="18"/>
<text text-anchor="middle" x="229" y="-175.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830&#45;&gt;p0x7f816f402a50</title>
<path d="M256.65,-232.76C252.29,-224.28 246.85,-213.71 241.96,-204.2"/>
<polygon points="244.99,-202.44 237.3,-195.15 238.77,-205.64 244.99,-202.44"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="193" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="193" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940&#45;&gt;p0x7f816f402a50</title>
<path d="M201.35,-232.76C205.71,-224.28 211.15,-213.71 216.04,-204.2"/>
<polygon points="219.23,-205.64 220.7,-195.15 213.01,-202.44 219.23,-205.64"/>
</g>
</g>
</svg>
</div><p>Debrief:</p><ul><li>Lines 1-12 create a taskflow of three tasks f1A, f1B, and f1C with f1A and f1B preceding f1C</li><li>Lines 17-30 create a taskflow of four tasks f2A, f2B, f2C, and f2D</li><li>Line 32 creates a module task from taskflow f1 through the method <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr />composed_of</a></li><li>Line 33 enforces task f2C to run before the module task</li><li>Line 34 enforces the module task to run before task f2D</li></ul></section><section id="CreateAModuleTask"><h2><a href="#CreateAModuleTask">Create a Module Task</a></h2><p>The task created from <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">Taskflow::<wbr />composed_of</a> is a <em>module</em> task that runs on a pre-defined taskflow. A module task does not own the taskflow but maintains a soft mapping to the taskflow. You can create multiple module tasks from the same taskflow but only one module task can run at one time. For example, the following composition is valid. Even though the two module tasks <code>module1</code> and <code>module2</code> refer to the same taskflow <code>F1</code>, the dependency link prevents <code>F1</code> from multiple executions at the same time.</p><div class="m-graph"><svg style="width: 31.600rem; height: 38.300rem;" viewBox="0.00 0.00 316.00 383.00">
<g transform="scale(1 1) rotate(0) translate(4 379)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-367 150,-367 150,-8 8,-8"/>
<text text-anchor="middle" x="79" y="-355" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="158,-224 158,-367 300,-367 300,-224 158,-224"/>
<text text-anchor="middle" x="229" y="-355" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="115" cy="-322" rx="27" ry="18"/>
<text text-anchor="middle" x="115" y="-319.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="79" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="79" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60&#45;&gt;p0x7f816f402d80</title>
<path d="M106.65,-304.76C102.29,-296.28 96.85,-285.71 91.96,-276.2"/>
<polygon points="94.99,-274.44 87.3,-267.15 88.77,-277.64 94.99,-274.44"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="140.5,-196 21.5,-196 17.5,-192 17.5,-160 136.5,-160 140.5,-164 140.5,-196"/>
<polyline points="136.5,-192 17.5,-192 "/>
<polyline points="136.5,-192 136.5,-160 "/>
<polyline points="136.5,-192 140.5,-196 "/>
<text text-anchor="middle" x="79" y="-175.5" font-family="Helvetica,sans-Serif" font-size="10.00">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80&#45;&gt;p0x7f816f402fa0</title>
<path d="M79,-231.7C79,-223.98 79,-214.71 79,-206.11"/>
<polygon points="82.5,-206.1 79,-196.1 75.5,-206.1 82.5,-206.1"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="43" cy="-322" rx="27" ry="18"/>
<text text-anchor="middle" x="43" y="-319.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70&#45;&gt;p0x7f816f402d80</title>
<path d="M51.35,-304.76C55.71,-296.28 61.15,-285.71 66.04,-276.2"/>
<polygon points="69.23,-277.64 70.7,-267.15 63.01,-274.44 69.23,-277.64"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa1</title>
<polygon points="140.5,-124 21.5,-124 17.5,-120 17.5,-88 136.5,-88 140.5,-92 140.5,-124"/>
<polyline points="136.5,-120 17.5,-120 "/>
<polyline points="136.5,-120 136.5,-88 "/>
<polyline points="136.5,-120 140.5,-124 "/>
<text text-anchor="middle" x="79" y="-103.5" font-family="Helvetica,sans-Serif" font-size="10.00">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0&#45;&gt;p0x7f816f402fa1</title>
<path d="M79,-159.7C79,-151.98 79,-142.71 79,-134.11"/>
<polygon points="82.5,-134.1 79,-124.1 75.5,-134.1 82.5,-134.1"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="79" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="79" y="-31.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa1&#45;&gt;p0x7f816f402e90</title>
<path d="M79,-87.7C79,-79.98 79,-70.71 79,-62.11"/>
<polygon points="82.5,-62.1 79,-52.1 75.5,-62.1 82.5,-62.1"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="265" cy="-322" rx="27" ry="18"/>
<text text-anchor="middle" x="265" y="-319.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="229" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="229" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830&#45;&gt;p0x7f816f402a50</title>
<path d="M256.65,-304.76C252.29,-296.28 246.85,-285.71 241.96,-276.2"/>
<polygon points="244.99,-274.44 237.3,-267.15 238.77,-277.64 244.99,-274.44"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="193" cy="-322" rx="27" ry="18"/>
<text text-anchor="middle" x="193" y="-319.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940&#45;&gt;p0x7f816f402a50</title>
<path d="M201.35,-304.76C205.71,-296.28 211.15,-285.71 216.04,-276.2"/>
<polygon points="219.23,-277.64 220.7,-267.15 213.01,-274.44 219.23,-277.64"/>
</g>
</g>
</svg>
</div><p>However, the following composition is <em>invalid</em>. Both module tasks refer to the same taskflow. They can not run at the same time because they are associated with the same graph.</p><div class="m-graph"><svg style="width: 45.500rem; height: 31.100rem;" viewBox="0.00 0.00 455.00 311.00">
<g transform="scale(1 1) rotate(0) translate(4 307)">
<title>Taskflow</title>
<g class="m-cluster">
<title>cluster_p0x7ffee9223970</title>
<polygon points="8,-8 8,-295 289,-295 289,-8 8,-8"/>
<text text-anchor="middle" x="148.5" y="-283" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F2</text>
</g>
<g class="m-cluster">
<title>cluster_p0x7ffee92238d0</title>
<polygon points="297,-152 297,-295 439,-295 439,-152 297,-152"/>
<text text-anchor="middle" x="368" y="-283" font-family="Helvetica,sans-Serif" font-size="10.00">Taskflow: F1</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402b60</title>
<ellipse cx="132" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="132" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402d80</title>
<ellipse cx="105" cy="-178" rx="27" ry="18"/>
<text text-anchor="middle" x="105" y="-175.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402b60&#45;&gt;p0x7f816f402d80</title>
<path d="M125.6,-232.41C122.49,-224.34 118.67,-214.43 115.17,-205.35"/>
<polygon points="118.4,-204.03 111.54,-195.96 111.87,-206.55 118.4,-204.03"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa0</title>
<polygon points="139.5,-124 20.5,-124 16.5,-120 16.5,-88 135.5,-88 139.5,-92 139.5,-124"/>
<polyline points="135.5,-120 16.5,-120 "/>
<polyline points="135.5,-120 135.5,-88 "/>
<polyline points="135.5,-120 139.5,-124 "/>
<text text-anchor="middle" x="78" y="-103.5" font-family="Helvetica,sans-Serif" font-size="10.00">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80&#45;&gt;p0x7f816f402fa0</title>
<path d="M98.6,-160.41C95.56,-152.54 91.86,-142.93 88.43,-134.04"/>
<polygon points="91.59,-132.52 84.73,-124.45 85.06,-135.04 91.59,-132.52"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402fa1</title>
<polygon points="280.5,-124 161.5,-124 157.5,-120 157.5,-88 276.5,-88 280.5,-92 280.5,-124"/>
<polyline points="276.5,-120 157.5,-120 "/>
<polyline points="276.5,-120 276.5,-88 "/>
<polyline points="276.5,-120 280.5,-124 "/>
<text text-anchor="middle" x="219" y="-103.5" font-family="Helvetica,sans-Serif" font-size="10.00">module [Taskflow: F1]</text>
</g>
<g class="m-edge">
<title>p0x7f816f402d80&#45;&gt;p0x7f816f402fa1</title>
<path d="M124.34,-165.13C140.2,-155.38 163.12,-141.32 182.42,-129.46"/>
<polygon points="184.31,-132.41 191,-124.19 180.65,-126.44 184.31,-132.41"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402c70</title>
<ellipse cx="60" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="60" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402c70&#45;&gt;p0x7f816f402d80</title>
<path d="M70.21,-233.12C75.85,-224.34 82.97,-213.26 89.3,-203.42"/>
<polygon points="92.37,-205.13 94.83,-194.82 86.48,-201.34 92.37,-205.13"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402e90</title>
<ellipse cx="148" cy="-34" rx="27" ry="18"/>
<text text-anchor="middle" x="148" y="-31.5" font-family="Helvetica,sans-Serif" font-size="10.00">f2D</text>
</g>
<g class="m-edge">
<title>p0x7f816f402fa0&#45;&gt;p0x7f816f402e90</title>
<path d="M95.3,-87.7C104.72,-78.28 116.47,-66.53 126.47,-56.53"/>
<polygon points="129.12,-58.82 133.72,-49.28 124.18,-53.88 129.12,-58.82"/>
</g>
<g class="m-edge">
<title>p0x7f816f402fa1&#45;&gt;p0x7f816f402e90</title>
<path d="M201.45,-87.7C191.89,-78.28 179.98,-66.53 169.83,-56.53"/>
<polygon points="172.06,-53.81 162.48,-49.28 167.15,-58.79 172.06,-53.81"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402830</title>
<ellipse cx="404" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="404" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1A</text>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402a50</title>
<ellipse cx="368" cy="-178" rx="27" ry="18"/>
<text text-anchor="middle" x="368" y="-175.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1C</text>
</g>
<g class="m-edge">
<title>p0x7f816f402830&#45;&gt;p0x7f816f402a50</title>
<path d="M395.65,-232.76C391.29,-224.28 385.85,-213.71 380.96,-204.2"/>
<polygon points="383.99,-202.44 376.3,-195.15 377.77,-205.64 383.99,-202.44"/>
</g>
<g class="m-node m-flat">
<title>p0x7f816f402940</title>
<ellipse cx="332" cy="-250" rx="27" ry="18"/>
<text text-anchor="middle" x="332" y="-247.5" font-family="Helvetica,sans-Serif" font-size="10.00">f1B</text>
</g>
<g class="m-edge">
<title>p0x7f816f402940&#45;&gt;p0x7f816f402a50</title>
<path d="M340.35,-232.76C344.71,-224.28 350.15,-213.71 355.04,-204.2"/>
<polygon points="358.23,-205.64 359.7,-195.15 352.01,-202.44 358.23,-205.64"/>
</g>
</g>
</svg>
</div></section><section id="CreateACustomComposableGraph"><h2><a href="#CreateACustomComposableGraph">Create a Custom Composable Graph</a></h2><p>Taskflow allows you to create a custom graph object that can participate in the scheduling using composition. To become a module task, your class <code>T</code> must define a method <code>T::graph()</code> that returns a reference to a <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> object. The following example defines a custom graph object that can be assembled in a taskflow throw composition:</p><pre class="m-code"><span class="w"> </span><span class="mi">1</span><span class="o">:</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">CustomGraph</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="mi">2</span><span class="o">:</span><span class="w">   </span><span class="n">tf</span><span class="o">::</span><span class="n">Graph</span><span class="w"> </span><span class="n">graph</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="mi">3</span><span class="o">:</span><span class="w">   </span><span class="n">CustomGraph</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="mi">4</span><span class="o">:</span><span class="w">     </span><span class="n">tf</span><span class="o">::</span><span class="n">FlowBuilder</span><span class="w"> </span><span class="n">builder</span><span class="p">(</span><span class="n">graph</span><span class="p">);</span><span class="w"></span>
<span class="w"> </span><span class="mi">5</span><span class="o">:</span><span class="w">     </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">task</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">builder</span><span class="p">.</span><span class="n">emplace</span><span class="p">([](){</span><span class="w"></span>
<span class="w"> </span><span class="mi">6</span><span class="o">:</span><span class="w">       </span><span class="n">std</span><span class="o">::</span><span class="n">cout</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="s">&quot;a task</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span><span class="w">  </span><span class="c1">// static task</span>
<span class="w"> </span><span class="mi">7</span><span class="o">:</span><span class="w">     </span><span class="p">});</span><span class="w"></span>
<span class="w"> </span><span class="mi">8</span><span class="o">:</span><span class="w">   </span><span class="p">}</span><span class="w"></span>
<span class="w"> </span><span class="mi">9</span><span class="o">:</span><span class="w">   </span><span class="c1">// returns a reference to the graph for taskflow composition</span>
<span class="mi">10</span><span class="o">:</span><span class="w">   </span><span class="n">Graph</span><span class="o">&amp;</span><span class="w"> </span><span class="n">graph</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="n">graph</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="mi">11</span><span class="o">:</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="mi">12</span><span class="o">:</span><span class="w"></span>
<span class="mi">13</span><span class="o">:</span><span class="w"> </span><span class="n">CustomGraph</span><span class="w"> </span><span class="n">obj</span><span class="p">;</span><span class="w"></span>
<span class="mi">14</span><span class="o">:</span><span class="w"> </span><span class="n">tf</span><span class="o">::</span><span class="n">Task</span><span class="w"> </span><span class="n">comp</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">taskflow</span><span class="p">.</span><span class="n">composed_of</span><span class="p">(</span><span class="n">obj</span><span class="p">);</span><span class="w"></span></pre><p>Debrief:</p><ul><li>Lines 1-11 define a custom graph interface to participate in taskflow composition</li><li>Line 2 defines the graph object using <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a></li><li>Lines 3-8 defines the constructor that constructs the task graph using <a href="classtf_1_1FlowBuilder.html" class="m-doc">tf::<wbr />FlowBuilder</a></li><li>Line 10 defines the required method for taskflow composition</li><li>Lines 13-14 creates a module task for the declared graph object in the taskflow</li></ul><p>The composition method <a href="classtf_1_1FlowBuilder.html#ac6f22228d4c2ea2e643c4b0d42c0e92a" class="m-doc">tf::<wbr />Taskflow::<wbr />composed_of</a> requires the target to define the <code>graph()</code> method that returns a reference to a <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> object defined by the target. At runtime, the executor will run dependent tasks in that graph using the same work-stealing scheduling algorithm as other taskflows. Taskflow leverages this powerful feature to design high-level algorithms, such as <a href="classtf_1_1Pipeline.html" class="m-doc">tf::<wbr />Pipeline</a>.</p><aside class="m-note m-info"><h4>Note</h4><p>While Taskflow gives you the flexibility to create a composable graph object, you should consider using <a href="classtf_1_1Graph.html" class="m-doc">tf::<wbr />Graph</a> as an opaque data structure just to interact with the library. Additionally, as other module tasks, Taskflow does not own the lifetime of a custom composable graph object but keeps a soft mapping to it. You should keep the graph object alive during its execution.</p></aside></section>
      </div>
    </div>
  </div>
</article></main>
<div class="m-doc-search" id="search">
  <a href="#!" onclick="return hideSearch()"></a>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-m-8 m-push-m-2">
        <div class="m-doc-search-header m-text m-small">
          <div><span class="m-label m-default">Tab</span> / <span class="m-label m-default">T</span> to search, <span class="m-label m-default">Esc</span> to close</div>
          <div id="search-symbolcount">&hellip;</div>
        </div>
        <div class="m-doc-search-content">
          <form>
            <input type="search" name="q" id="search-input" placeholder="Loading &hellip;" disabled="disabled" autofocus="autofocus" autocomplete="off" spellcheck="false" />
          </form>
          <noscript class="m-text m-danger m-text-center">Unlike everything else in the docs, the search functionality <em>requires</em> JavaScript.</noscript>
          <div id="search-help" class="m-text m-dim m-text-center">
            <p class="m-noindent">Search for symbols, directories, files, pages or
            modules. You can omit any prefix from the symbol or file path; adding a
            <code>:</code> or <code>/</code> suffix lists all members of given symbol or
            directory.</p>
            <p class="m-noindent">Use <span class="m-label m-dim">&darr;</span>
            / <span class="m-label m-dim">&uarr;</span> to navigate through the list,
            <span class="m-label m-dim">Enter</span> to go.
            <span class="m-label m-dim">Tab</span> autocompletes common prefix, you can
            copy a link to the result using <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">L</span> while <span class="m-label m-dim">⌘</span>
            <span class="m-label m-dim">M</span> produces a Markdown link.</p>
          </div>
          <div id="search-notfound" class="m-text m-warning m-text-center">Sorry, nothing was found.</div>
          <ul id="search-results"></ul>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="search-v2.js"></script>
<script src="searchdata-v2.js" async="async"></script>
<footer><nav>
  <div class="m-container">
    <div class="m-row">
      <div class="m-col-l-10 m-push-l-1">
        <p>Taskflow handbook is part of the <a href="https://taskflow.github.io">Taskflow project</a>, copyright © <a href="https://tsung-wei-huang.github.io/">Dr. Tsung-Wei Huang</a>, 2018&ndash;2023.<br />Generated by <a href="https://doxygen.org/">Doxygen</a> 1.9.1 and <a href="https://mcss.mosra.cz/">m.css</a>.</p>
      </div>
    </div>
  </div>
</nav></footer>
</body>
</html>
